package com.Lgwd.service.backService.backServiceImpl;

import com.Lgwd.dao.DropDao;
import com.Lgwd.dao.ItemDao;
import com.Lgwd.entity.BackInfoEntity;
import com.Lgwd.entity.DropGoodsEntity;
import com.Lgwd.entity.InitializeProfessionEntiy;
import com.Lgwd.entity.ItemPropertyEntity;
import com.Lgwd.repository.BackInfoRepository;
import com.Lgwd.repository.DropGoodsRepository;
import com.Lgwd.service.backService.BackInfoService;
import com.Lgwd.service.backService.DropGoodsService;
import com.Lgwd.service.backService.ItemPropertyService;
import com.Lgwd.service.backService.UserItemInfoService;
import com.Lgwd.utils.RandomUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class DropGoodsServiceImpl implements DropGoodsService {


    @Autowired
    DropGoodsRepository dropGoodsRepository;

    /**
     * 背包仓库
     */
    @Autowired
    BackInfoService backInfoService;

    /**
     * 背包里的 装备属性
     */
    @Autowired
    UserItemInfoService userItemInfoService;
    /**
     * 装备属性表数据
     */
    @Autowired
    ItemPropertyService itemPropertyService;

    /**
     * 根据返回账号 提供掉落物品 加入到背包
     *
     * @param dropDao
     * @return
     */
    @Override
    public ItemDao returnGoods(DropDao dropDao) {
        List<DropGoodsEntity> list = dropGoodsRepository.findByLv_scope(dropLvScope(dropDao.getLv()));
        //利用随机数决定掉不掉落该装备
        if (list.size()!=0&&flag()) {
            //获取查询出来的list列表中其中一个装备
            DropGoodsEntity dropGoodsEntity = list.get(RandomUtils.getRangeStr(0, list.size()));
            //获取装备属性根据item_code_id 查询出 该装备属性值
            ItemPropertyEntity itemPropertyEntity = itemPropertyService.findByItemCodeId(dropGoodsEntity.getItem_code_id());
            //设置装备品级随机
            //随机数范围1 2 3
            Integer i = RandomUtils.getRangeStr(1, 3);
            ItemPropertyEntity newItem = propertyInfo(itemPropertyEntity, i);
            String item_id = RandomUtils.getRandom(6)+i;
            //新装备的属性放到角色背包表中
            userItemInfoService.addItemToBack(newItem, dropDao.getUser_account(), i,item_id,dropGoodsEntity.getItem_code_id());
            //封装方法返回数据
            ItemDao itemDao = new ItemDao();
            itemDao.setItem_lv(dropGoodsEntity.getItem_lv());
            itemDao.setProfession(dropGoodsEntity.getItem_profession());
            itemDao.setProperty_quality(i);
            BeanUtils.copyProperties(newItem, itemDao);
            //保存到用户背包
            backInfoService.addItemToUserBack(itemDao, dropDao.getUser_account(),item_id);
            return itemDao;
        }
        return null;
    }



    /**
     * 根据装备属性对装备进行品级属性赋值
     * 1.下品  最初的值
     * 2.中品  相对于下品高出10%
     * 3.上品 相对于下品高出30%
     */

    public ItemPropertyEntity propertyInfo(ItemPropertyEntity itemPropertyEntity, Integer i) {
        if (i == 1) {
            //下品值 直接返回
            return calculate(itemPropertyEntity, 0.0, i);
        }
        if (i == 2) {
            //中品值全属性+10%
            return calculate(itemPropertyEntity, 0.1, i);
        }
        if (i == 3) {
            //上品值全属性+30%
            return calculate(itemPropertyEntity, 0.3, i);
        }
        return itemPropertyEntity;
    }


    /**
     * 装备品质计算方法
     *
     * @param itemPropertyEntity
     * @param i
     * @return
     */
    public ItemPropertyEntity calculate(ItemPropertyEntity itemPropertyEntity, double i, Integer integer) {
        ItemPropertyEntity info = new ItemPropertyEntity();
        info.setProperty_quality(integer);
        info.setItem_name(itemPropertyEntity.getItem_name());
        info.setItem_code_id(itemPropertyEntity.getItem_code_id());
        info.setMoral_type_id(itemPropertyEntity.getMoral_type_id());
        info.setItem_profession(itemPropertyEntity.getItem_profession());
        info.setProperty_A((int) Math.round(itemPropertyEntity.getProperty_A() + (itemPropertyEntity.getProperty_A() * i)));
        info.setProperty_DF((int) Math.round(itemPropertyEntity.getProperty_DF() + (itemPropertyEntity.getProperty_DF() * i)));
        info.setProperty_HP((int) Math.round(itemPropertyEntity.getProperty_HP() + (itemPropertyEntity.getProperty_HP() * i)));
        info.setProperty_M((int) Math.round(itemPropertyEntity.getProperty_M() + (itemPropertyEntity.getProperty_M() * i)));
        info.setProperty_MP((int) Math.round(itemPropertyEntity.getProperty_MP() + (itemPropertyEntity.getProperty_MP() * i)));
        info.setProperty_P((int) Math.round(itemPropertyEntity.getProperty_P() + (itemPropertyEntity.getProperty_P() * i)));
        return info;
    }


    /**
     * 掉落概率为20%
     *
     * @return
     */
    public boolean flag() {
        int i = RandomUtils.getRangeStr(0, 10);

        if (i <= 1) {

            return true;
        } else {

            return false;
        }
    }

    /**
     * 计算掉落等级范围
     *
     * @param userLv
     * @return
     */
    public Integer dropLvScope(Integer userLv) {
        if (userLv >= 0 && userLv <= 3) {
            return 3;
        }
        if (userLv >= 4 && userLv <= 6) {
            return 6;
        }
        if (userLv >= 7 && userLv <= 9) {
            return 9;
        }
        if (userLv >= 10 && userLv <= 12) {
            return 12;
        }
        if (userLv >= 13 && userLv <= 15) {
            return 15;
        }
        if (userLv >= 16 && userLv <= 18) {
            return 18;
        }
        if (userLv >= 19 && userLv <= 21) {
            return 21;
        }
        if (userLv >= 22 && userLv <= 24) {
            return 24;
        }
        if (userLv >= 25 && userLv <= 27) {
            return 27;
        }
        if (userLv >= 28 && userLv <= 30) {
            return 30;
        }
        return 0;
    }
}
